package chapter20;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @program: exercise
 * @description: 20-1， 使用了定义在collection接口中方法的实例。
 * @author: 陈庆彪
 * @create: 2020-02-21 09:11
 **/
public class TestCollection {
    public static void main(String[] args) {
        ArrayList<String> collection1=new ArrayList<>();
        collection1.add("北京");
        collection1.add("上海");
        collection1.add("武汉");
        collection1.add("南京");
        collection1.add("杭州");
        System.out.println("a list of cities in collection1: ");
        System.out.println(collection1);
        System.out.println("\nIs 武汉 in collection1？"+collection1.contains("武汉"));

        collection1.remove("武汉");
        System.out.println("\n"+collection1.size()+" cities are in collection1 now"+"\n");

        Collection<String> collection2=new ArrayList<>();
        collection2.add("黄冈");
        collection2.add("黄石");
        collection2.add("鄂州");
        collection2.add("孝感");
        collection2.add("荆州");
        System.out.println("a list of cities in collection2: ");
        System.out.println(collection2);

        ArrayList<String> c1= (ArrayList<String>) collection1.clone();
        c1.addAll(collection2);
        System.out.println("cities in collection1 or collection2: "); //求合集
        System.out.println(c1);

//        c1= (ArrayList<String>) collection1.clone();
//        c1.retainAll(collection2);//批量删除c1中，不包含collection2中元素的所有元素。求collection1和collection2的交集
//        System.out.println("cities in collection1 and collection2: ");
//        System.out.println(c1);

        c1= (ArrayList<String>) collection1.clone();
        c1.removeAll(collection2);//批量删除c1中，包含collection2元素的所有元素。
        System.out.println("cities in collection1,but not in collection2: ");
        System.out.println(c1);


    }
}
